package com.oom;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * @description: 直接内存溢出, 使用unsafe类分配直接内存
 * VM args:-Xmx20M -XX:MaxDirectMemorySize=10M
 * @author: yuchi_huang (yuchi_huang@sui.com)
 * @date: 18/3/15
 * @modified By:
 */
public class DirectMemoryOOM {

    private static final int _1MB = 1024*1024*1024*1024;

    public static void main(String[] args) throws Exception {

        Field unsafeField = Unsafe.class.getDeclaredFields()[0];
        unsafeField.setAccessible(true);

        Unsafe unsafe = (Unsafe)unsafeField.get(null);
        int i = 0;
        while (true) {
            System.out.println(i++);
            unsafe.allocateMemory(_1MB);
        }

    }

}
